package com.example.zengyan.myphonebook;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.Bundler;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import BD.SQLiteUtil;
import BD.mConfig;
import Bean.Organization;
import fragments.ListFragment;

import static BD.mConfig.*;

public class FragmentActivity extends AppCompatActivity {
    private int id;



    public static void startActivity(Context context, int id) {

        Intent intent = new Intent(context, FragmentActivity.class);
        intent.putExtra(KEY_ID, id);
        context.startActivity(intent);
    }

    private int getID() {
//        return getIntent().getStringExtra(mConfig.KEY_ID);
        return getIntent().getIntExtra(KEY_ID,0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        id=getID();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

//        ArrayList<Organization> allO= (ArrayList<Organization>) DataSupport.findAll(Organization.class,true);
        ArrayList<Bean.Member> allM= (ArrayList<Bean.Member>) DataSupport.findAll(Bean.Member.class,true);
        Organization organization= SQLiteUtil.getOrganiztionById(id);

        toolbar.setTitle(organization.getOaccount());

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ViewGroup tab = (ViewGroup) findViewById(R.id.tab);
        tab.addView(LayoutInflater.from(this).inflate(R.layout.demo_indicator_trick1, tab, false));

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);

        FragmentPagerItems.Creator creator=FragmentPagerItems.with(this);
        List<String> allD= SQLiteUtil.getAllDepartment(organization);

        for (String s:allD ) {
            creator .add(s, ListFragment.class, new Bundler().putString(mConfig.KEY_DEPARTMENT, s).putInt(KEY_ID,id).get());
        }

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(),creator.create());

        viewPager.setAdapter(adapter);
        viewPagerTab.setViewPager(viewPager);


    }
}
